package com.linkage.jz.bankruptcy.activity.Main;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.MainActivity;
import com.linkage.jz.bankruptcy.activity.Person.PersonSettingReadMeActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.GetPwdOneActivity;
import com.linkage.jz.bankruptcy.activity.RegistLogin.PhoneLoginActivity;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.fragment.Main.DocTmpGFrgment;
import com.linkage.jz.bankruptcy.fragment.Main.DocTmpZFrgment;
import com.linkage.jz.bankruptcy.fragment.Main.LawListFrgment;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;

import java.util.ArrayList;


/**
 * A login screen that offers login via email/password.
 */
public  class DocTmpListActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = PersonSettingReadMeActivity.class.getSimpleName();
    private Button topBtn01, topBtn02;
    private LinearLayout topBtnContainer;
    private ViewPager mViewPager;
    private TabPagerAdapter mPagerAdapter;
    ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private RelativeLayout rl_search2,rl_search1;
    private ShapeLoadingDialog shapeLoadingDialog ;
    private EditText law_searchtxt;
    @Override
    public void initData() {

    }

    @Override
    public void setListener() {

        top_back.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        top_title.setText("文本模板");
        top_mune.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initView() {

    }


    TextView top_title;
    Button top_back,top_mune;

    public static void start(Context context) {
        Intent intent = new Intent(context, DocTmpListActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_main_doctmplist);

        initview();
        shapeLoadingDialog = new ShapeLoadingDialog(this);
        ActionBar bar = this.getActionBar();
        if(bar != null){
            bar.hide();
        }

        init();
        super.onCreate(savedInstanceState);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层



        top_back = findViewById(R.id.top_back);
        top_mune = findViewById(R.id.top_mune);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                //返回
                MainActivity.start(this);
                finish();
                break;
            case R.id.ll_phonelogin:
                PhoneLoginActivity.start(this);
                finish();
                break;
            case R.id.top_back:
                finish();
                break;
            case R.id.getPwd:
                GetPwdOneActivity.start(this);
            case R.id.id_dcphGroup_topBtn01:
                changeFragmentFocus(0);
                mViewPager.setCurrentItem(0);
                break;
            case R.id.id_dcphGroup_topBtn02:
                changeFragmentFocus(1);
                mViewPager.setCurrentItem(1);
                break;
            default:
                break;
        }
    }

    private int showWeb(String url) {
        int ret = -1;
        try {
            Uri uri = Uri.parse(url);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
            ret = 0;
        } catch (Exception e) {

        }
        return ret;
    }

    private void init(){
        DocTmpZFrgment groupChildrenCollect = new DocTmpZFrgment();
        DocTmpGFrgment groupChildrenHasBuy = new DocTmpGFrgment();
        fragments.add(groupChildrenCollect);
        fragments.add(groupChildrenHasBuy);

        topBtn01 = (Button) this.findViewById(R.id.id_dcphGroup_topBtn01);
        topBtn02 = (Button) this.findViewById(R.id.id_dcphGroup_topBtn02);
        topBtnContainer = (LinearLayout) this.findViewById(R.id.id_dcphGroup_topBtn_container);
        rl_search1=findViewById(R.id.rl_search1);
        rl_search2=findViewById(R.id.rl_search2);

        rl_search2.setVisibility(View.GONE);


        topBtn01.setOnClickListener(this);
        topBtn02.setOnClickListener(this);
        mViewPager = (ViewPager) this.findViewById(R.id.id_dcphGroup_viewPager);
        mPagerAdapter = new TabPagerAdapter(this.getSupportFragmentManager(), fragments);

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.invalidate();
        mPagerAdapter.notifyDataSetChanged();


//        law_searchtxt.setOnEditorActionListener(new TextView.OnEditorActionListener(){
//            @Override
//
//            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
//
//// TODO Auto-generated method stub
//
//                if(arg1 == EditorInfo.IME_ACTION_SEARCH)
//
//                {
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    if (imm != null) {
//                        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
//                    }
//
//
//                }
//
//                return false;
//
//            }
//
//        });
    }

    /*
     * 自定义FragmentStatePagerAdapter适配器来显示滑动Fragment
     * */
    private class TabPagerAdapter extends FragmentStatePagerAdapter implements
            ViewPager.OnPageChangeListener {

        private ArrayList<Fragment> objects;

        public TabPagerAdapter(FragmentManager fm, ArrayList<Fragment> objects) {
            super(fm);
            this.objects = objects;
            mViewPager.setOnPageChangeListener(this);
        }

        @Override
        public Fragment getItem(int position) {
            return objects.get(position);
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            //滑动的监听事件
            changeFragmentFocus(position);
        }
    }


    private void changeFragmentFocus(int focus){
        if(focus == 0){
            topBtn01.setBackgroundResource(R.drawable.about_b_corner_btn);
            topBtn02.setBackgroundResource(R.drawable.about_g_corner_btn);
            topBtn01.setTextColor(getResources().getColor(R.color.white));
            topBtn02.setTextColor(getResources().getColor(R.color.text_top_color));
            rl_search1.setVisibility(View.VISIBLE);
            rl_search2.setVisibility(View.GONE);
        }else if (focus == 1){
            rl_search2.setVisibility(View.VISIBLE);
            rl_search1.setVisibility(View.GONE);
            topBtn01.setBackgroundResource(R.drawable.about_g_corner_btn);
            topBtn02.setBackgroundResource(R.drawable.about_b_corner_btn);
            topBtn01.setTextColor(getResources().getColor(R.color.text_top_color));
            topBtn02.setTextColor(getResources().getColor(R.color.white));
        }
    }

}


